package com.atguigu.process.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.atguigu.auth.service.SysUserService;
import com.atguigu.model.process.Process;
import com.atguigu.model.process.ProcessRecord;
import com.atguigu.model.process.ProcessTemplate;
import com.atguigu.model.system.SysUser;
import com.atguigu.process.mapper.OaProcessMapper;
import com.atguigu.process.service.MessageService;
import com.atguigu.process.service.OaProcessRecordService;
import com.atguigu.process.service.OaProcessService;
import com.atguigu.process.service.OaProcessTemplateService;
import com.atguigu.security.custom.LoginUserInfoHelper;
import com.atguigu.vo.process.ApprovalVo;
import com.atguigu.vo.process.ProcessFormVo;
import com.atguigu.vo.process.ProcessQueryVo;
import com.atguigu.vo.process.ProcessVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.FlowNode;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.zip.ZipInputStream;

/**
 * <p>
 * 审批类型 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-04-22
 */
@Service
public class OaProcessServiceImpl extends ServiceImpl<OaProcessMapper, Process> implements OaProcessService {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private OaProcessTemplateService processTemplateService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private OaProcessRecordService processRecordService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private MessageService messageService;

    /**
     * 分页查询
     *
     * @param pageParam      {@link IPage<ProcessVo>}
     * @param processQueryVo {@link ProcessQueryVo}
     * @return {@link IPage<ProcessVo>}
     */
    @Override
    public IPage<ProcessVo> selectPage(IPage<ProcessVo> pageParam, ProcessQueryVo processQueryVo) {
        return baseMapper.selectPage(pageParam, processQueryVo);
    }

    /**
     * 部署流程定义
     *
     * @param deployPath {@link String}
     */
    @Override
    public void deployByZip(String deployPath) {
        try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(deployPath)) {
            ZipInputStream zipInputStream = new ZipInputStream(Objects.requireNonNull(inputStream));
            // 部署
            Deployment deploy = repositoryService.createDeployment().addZipInputStream(zipInputStream).deploy();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 启动流程实例
     *
     * @param processFormVo {@link ProcessFormVo}
     */
    @Override
    public void startUp(ProcessFormVo processFormVo) {
        // 1.根据当前用户 bussinessKey 获取用户信息
        SysUser sysUser = sysUserService.getById(LoginUserInfoHelper.getUserId());

        // 2.根据审批模板 bussinessKey 把模板信息查询
        ProcessTemplate processTemplate = processTemplateService.getById(processFormVo.getProcessTemplateId());

        // 3.保存提交的审批信息到业务表 oa_process
        Process process = new Process();
        // processFormVo 复制到 process 对象里面
        BeanUtils.copyProperties(processFormVo, process);
        // 其他值
        process.setStatus(1);
        String workNo = String.valueOf(System.currentTimeMillis());
        process.setProcessCode(workNo);
        process.setUserId(LoginUserInfoHelper.getUserId());
        process.setFormValues(processFormVo.getFormValues());
        process.setTitle(sysUser.getName() + "发起" + processTemplate.getName());
        save(process);

        // 4.启动流程实例 - RuntimeService
        // 4.1 流程定义 key
        String processDefinitionKey = processTemplate.getProcessDefinitionKey();

        // 4.2 业务 key processId
        String businessKey = String.valueOf(process.getId());

        // 4.3 流程参数 form 表单参数 json 数据，转换 map 集合
        String formValues = processFormVo.getFormValues();
        // formData
        JSONObject jsonObject = JSON.parseObject(formValues);
        JSONObject formData = jsonObject.getJSONObject("formData");
        // 遍历 formData 得到内容，封装 map 集合
        Map<String, Object> map = new HashMap<>(formData);
        Map<String, Object> variables = new HashMap<>();
        variables.put("data", map);
        // 启动流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);

        // 5.查询下一个审批人
        // 审批人可能会有多个
        List<Task> taskList = this.getCurrentTaskList(processInstance.getId());
        List<String> nameList = new ArrayList<>();
        taskList.forEach(task -> {
            String assigneeName = task.getAssignee();
            SysUser user = sysUserService.getUserByUserName(assigneeName);
            String name = user.getName();
            nameList.add(name);
            // 推送消息
            messageService.pushPendingMessage(process.getId(), user.getId(), task.getId());
        });
        process.setProcessInstanceId(processInstance.getId());
        process.setDescription("等待" + StringUtils.join(nameList, ",") + "审批");
        // 7.业务和流程关联 更新 oa_process 数据
        updateById(process);

        // 记录操作审批信息记录
        processRecordService.record(process.getId(), 1, "发起申请");
    }

    /**
     * 查询代办任务列表
     *
     * @param pageParam {@link IPage<Process>}
     * @return {@link IPage<Process>}
     */
    @Override
    public IPage<ProcessVo> findPending(IPage<Process> pageParam) {
        // 1.封装查询条件，根据当前登录的用户名称
        TaskQuery query = taskService.createTaskQuery()
                .taskAssignee(LoginUserInfoHelper.getUsername())
                .orderByTaskCreateTime()
                .desc();

        // 2.调用方法分页条件查询，返回 list 集合，代办任务集合
        // listPage 方法有两个参数
        // 第一个参数：开始位置   第二个参数：每页显示记录数
        int begin = (int) ((pageParam.getCurrent() - 1) * pageParam.getSize());
        int size = (int) pageParam.getSize();
        List<Task> taskList = query.listPage(begin, size);
        long totalCount = query.count();
        // 3.封装返回 list 集合数据到 List<ProcessVo>
        // List<task> ---> List<ProcessVo>
        ArrayList<ProcessVo> processVoList = new ArrayList<>();
        taskList.forEach(task -> {
            // 从 task 获取流程实例 id
            String processInstanceId = task.getProcessInstanceId();
            // 根据流程实例 id 获取实例对象
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(processInstanceId)
                    .singleResult();
            // 从流程实例对象获取业务 key -- processId
            String businessKey = processInstance.getBusinessKey();
            if (businessKey == null || "".equals(businessKey)) {
                return;
            }
            // 根据业务 key 获取 Process 对象
            long processId = Long.parseLong(businessKey);
            Process process = getById(processId);
            ProcessVo processVo = new ProcessVo();
            // Process 对象复制到 ProcessVo 对象
            BeanUtils.copyProperties(process, processVo);
            processVo.setTaskId(task.getId());
            // 放到最终 list 集合 processVoList
            processVoList.add(processVo);
        });

        // 4.封装返回 Page 对象
        IPage<ProcessVo> page = new Page<>(pageParam.getCurrent(), pageParam.getSize(), totalCount);
        page.setRecords(processVoList);
        return page;
    }

    /**
     * 查看审批详情信息
     *
     * @param id {@link Long}
     * @return {@link Map<String, Object>}
     */
    @Override
    public Map<String, Object> show(Long id) {
        // 1.根据流程 id 获取流程信息 Process
        Process process = getById(id);

        // 2.根据流程 id 获取流程记录信息
        LambdaQueryWrapper<ProcessRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcessRecord::getProcessId, id);
        List<ProcessRecord> processRecordList = processRecordService.list(wrapper);

        // 3.根据模板 id 查询模板信息
        ProcessTemplate processTemplate = processTemplateService.getById(process.getProcessTemplateId());

        // 4.判断当前用户是否可以审批
        // 可以看到信息不一定能审批，不能重复审批
        boolean isApprove = false;
        List<Task> taskList = this.getCurrentTaskList(process.getProcessInstanceId());
        for (Task task : taskList) {
            String username = LoginUserInfoHelper.getUsername();
            if (task.getAssignee().equals(username)) {
                isApprove = true;
                break;
            }
        }
        // 5.查询数据封装到 map 集合，返回
        Map<String, Object> map = new HashMap<>();
        map.put("process", process);
        map.put("processRecordList", processRecordList);
        map.put("processTemplate", processTemplate);
        map.put("isApprove", isApprove);
        return map;
    }

    /**
     * 审批
     *
     * @param approvalVo {@link ApprovalVo}
     */
    @Override
    public void approve(ApprovalVo approvalVo) {
        // 1.从 approvalVo 获取任务 id，根据任务 id 获取流程变量
        String taskId = approvalVo.getTaskId();
        Map<String, Object> variables = taskService.getVariables(taskId);

        // 2.判断审批状态值
        if (approvalVo.getStatus() == 1) {
            // 2.1.状态值 = 1 审批通过
            HashMap<String, Object> variable = new HashMap<>();
            taskService.complete(taskId, variable);
        } else {
            // 2.2.状态值 = -1 驳回，流程直接结束
            this.endTask(taskId);
        }

        // 3.记录审批相关过程信息 oa_process_record
        String description = approvalVo.getStatus() == 1 ? "已通过" : "驳回";
        processRecordService.record(approvalVo.getProcessId(), approvalVo.getStatus(), description);

        // 4.查询下一个审批人，更新流程表记录 process 表记录
        Process process = getById(approvalVo.getProcessId());

        // 5.查询任务
        List<Task> taskList = getCurrentTaskList(process.getProcessInstanceId());
        if (!CollectionUtils.isEmpty(taskList)) {
            List<String> assignList = new ArrayList<>();
            taskList.forEach(task -> {
                String assignee = task.getAssignee();
                SysUser sysUser = sysUserService.getUserByUserName(assignee);
                assignList.add(sysUser.getName());

                // todo 公众号消息推送
            });
            // 更新 process 流程信息
            process.setDescription("等待" + StringUtils.join(assignList, ",") + "审批");
            process.setStatus(1);
        } else {
            if (approvalVo.getStatus() == 1) {
                process.setDescription("审批完成(通过)");
                process.setStatus(2);
            } else {
                process.setDescription("审批完成(驳回)");
                process.setStatus(-1);
            }
        }
        updateById(process);
    }

    /**
     * 已处理
     *
     * @param pageParam {@link IPage< java.lang.Process >}
     * @return {@link IPage<ProcessVo>}
     */
    @Override
    public IPage<ProcessVo> findProcessed(IPage<Process> pageParam) {
        // 封装查询条件
        HistoricTaskInstanceQuery query = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee(LoginUserInfoHelper.getUsername())
                .finished().orderByTaskCreateTime().desc();

        // 调用方法条件分页查询，返回 list 集合
        // 开始位置和每页显示记录数
        int begin = (int) ((pageParam.getCurrent() - 1) * pageParam.getSize());
        int size = (int) pageParam.getSize();
        List<HistoricTaskInstance> list = query.listPage(begin, size);
        long totalCount = query.count();

        // 遍历返回 list 集合，封装 List<ProcessVo>
        ArrayList<ProcessVo> processVoList = new ArrayList<>();
        list.forEach(item -> {
            // 获取流程实例 id
            String processInstanceId = item.getProcessInstanceId();
            // 根据流程实例 id 查询获取 process 信息
            LambdaQueryWrapper<Process> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Process::getProcessInstanceId, processInstanceId);
            Process process = getOne(wrapper);
            // process --> processVo
            ProcessVo processVo = new ProcessVo();
            BeanUtils.copyProperties(process, processVo);
            // 放到 list
            processVoList.add(processVo);
        });
        // IPage 封装分页查询所有数据，返回
        IPage<ProcessVo> pageModel = new Page<>(pageParam.getCurrent(), pageParam.getSize(), totalCount);
        pageModel.setRecords(processVoList);
        return pageModel;
    }

    /**
     * 查询发起的审批
     *
     * @param pageParam {@link IPage< java.lang.Process >}
     * @return {@link IPage<ProcessVo>}
     */
    @Override
    public IPage<ProcessVo> findStarted(Page<ProcessVo> pageParam) {
        ProcessQueryVo processQueryVo = new ProcessQueryVo();
        processQueryVo.setUserId(LoginUserInfoHelper.getUserId());
        return baseMapper.selectPage(pageParam, processQueryVo);
    }

    /**
     * 结束流程
     *
     * @param taskId 任务 id
     */
    private void endTask(String taskId) {
        // 1.根据任务 id 获取任务对象 Task
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();

        // 2.获取流程定义模型 BpmnModel
        BpmnModel bpmnModel = repositoryService.getBpmnModel(task.getProcessDefinitionId());

        // 3.获取结束流向节点
        List<EndEvent> endEventList = bpmnModel.getMainProcess().findFlowElementsOfType(EndEvent.class);
        if (CollectionUtils.isEmpty(endEventList)) {
            return;
        }
        FlowNode endFlowNode = endEventList.get(0);

        // 4.当前流向节点
        FlowNode currentFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(task.getTaskDefinitionKey());

        // 临时保存当前活动的原始方向
        ArrayList<SequenceFlow> originalSequenceFlowList = new ArrayList<>(currentFlowNode.getOutgoingFlows());
        // 5.清理当前流动方向
        currentFlowNode.getOutgoingFlows().clear();

        // 6.创建新流向
        SequenceFlow newSequenceFlow = new SequenceFlow();
        newSequenceFlow.setId("newSequenceFlow");
        newSequenceFlow.setSourceFlowElement(currentFlowNode);
        newSequenceFlow.setTargetFlowElement(endFlowNode);

        // 7.当前节点指向新方向
        List<SequenceFlow> newSequenceFlowList = new ArrayList<>();
        newSequenceFlowList.add(newSequenceFlow);
        currentFlowNode.setOutgoingFlows(newSequenceFlowList);

        // 8.完成当前任务
        taskService.complete(taskId);
    }

    /**
     * 当前任务列表
     *
     * @param id 流程实例 id
     * @return {@link List<Task>}
     */
    private List<Task> getCurrentTaskList(String id) {
        return taskService.createTaskQuery().processInstanceId(id).list();
    }
}
